package com.github.alenfive.examples;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

import javax.script.*;

import java.util.HashMap;
import java.util.Map;

import static com.github.alenfive.utils.DataSourceUtils.jdbcTemplate;

public class GroovyTest {

    public static void main(String[] args) throws Exception {

        testSqlInjectJdbcTemplate();

        testSqlJdbcTemplateImport();

        testGroovyScript();

        testGroovyClass();
    }

    public static void testGroovyClass() throws Exception {
        String scriptStr = "" +
                "       def xx(num) {\n" +
                "            return num.num>0?num.num:0;\n" +
                "        }\n" +
                "        return xx(num);";
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        Class clazz = groovyClassLoader.parseClass(scriptStr);
        GroovyObject clazzObj =(GroovyObject)clazz.newInstance();
        Map<String, Object> params = new HashMap();
        params.put("num", 30);
        Object value = clazzObj.invokeMethod("xx",params);
        System.out.println(value);
    }

    public static void testGroovyScript() throws Exception {
        String scriptStr = "" +
                "       def xx(num){\n" +
                "            return num>0?num:0;\n" +
                "        }\n" +
                "        return xx(num);";

        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy"); //2000000次 24159 23388 22056 22195
        CompiledScript compiledScript = ((Compilable) engine).compile(scriptStr); //2000000次 22943 22005 23711
        Bindings bindings = new SimpleBindings();
        bindings.put("num", 30);
        Object value = compiledScript.eval(bindings);
        System.out.println(value);
    }

    /**
     * 在bindings变量中注入jdbcTemplate
     * bindings.put("jdbcTemplate", jdbcTemplate);
     * @throws Exception
     */
    public static void testSqlInjectJdbcTemplate() throws Exception {
        String script = "Map<String, Object> params = new HashMap();\n" +
                "params.put('id', id);\n" +
                "Object obj = jdbcTemplate.queryForList('SELECT * FROM api_info WHERE id=:id LIMIT 1', params);\n" +
                "return obj\n";
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");
        Bindings bindings = new SimpleBindings();
        bindings.put("jdbcTemplate", jdbcTemplate);
        bindings.put("id", "62085f2016412c4b3a2ddab1");
        Object object = engine.eval(script,bindings);
        System.out.println(JSON.toJSONString(object, SerializerFeature.PrettyFormat));
    }

    /**
     * 在脚本中用import引入jdbctemplate
     * import static com.github.alenfive.utils.DataSourceUtils.jdbcTemplate;
     * @throws Exception
     */
    public static void testSqlJdbcTemplateImport() throws Exception {
        String script = "import static com.github.alenfive.utils.DataSourceUtils.jdbcTemplate;\n" +
                "\n" +
                "Map<String, Object> params = new HashMap();\n" +
                "params.put('id', id);\n" +
                "Object obj = jdbcTemplate.queryForList('SELECT * FROM api_info WHERE id=:id LIMIT 1', params);\n" +
                "return obj\n";
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");
        Bindings bindings = new SimpleBindings();
        bindings.put("id", "62085f2016412c4b3a2ddab1");
        Object object = engine.eval(script,bindings);
        System.out.println(JSON.toJSONString(object, SerializerFeature.PrettyFormat));
    }

}
